/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.osdtsystem.hierarchy;

import org.osdtsystem.utils.Storage;

/**
 * A prefix hierarchy.
 * @author Matthias Buch-Kromann
 */
public class PrefixHierarchy extends AbstractAffixHierarchy {
    public PrefixHierarchy(Storage dataStorage) {
        super(dataStorage);
    }

    @Override
    public AffixHClass intersectionBinary(AffixHClass hc1, AffixHClass hc2) {
        // Return empty class if either class is empty
        if (isEmpty(hc1) || isEmpty(hc2))
            return emptyClass();

        // Retrieve affixes
        String prefix1 = hc1.affix();
        String prefix2 = hc2.affix();

        // Class2 is a subclass of class1
        if (prefix2.length() >= prefix1.length() && prefix2.startsWith(prefix1))
            return hc2;


        // class1 is a subclass of class2
        if (prefix1.length() >= prefix2.length() && prefix1.startsWith(prefix2))
            return hc1;

        // Otherwise return empty class
        return emptyClass();
    }

    @Override
    public AffixHClass superclass(String prefix, int depth) {
        // Return terminal class if depth larger than affix length
        if (depth > prefix.length())
            return makeAffixClass(prefix, true);

        // Return subaffix otherwise
        return makeAffixClass(prefix.substring(0, depth), false);
    }
}
